import pandas as pd
import random
from pyecharts import options as opts
from pyecharts.charts import HeatMap, Page, Line
from pyecharts.components import Table



def create_heatmap(result_df, label):
    pivot_df = result_df.pivot_table(
        values=f'{label}',
        index='oi_s',  # Y轴作为行
        columns='oi_l',  # X轴作为列
        aggfunc='mean'  # 处理重复值：取平均值
    )
    rows = len(pivot_df)
    cols = len(pivot_df.columns)
    datas = []
    for i in range(rows):
        for j in range(cols):
            datas.append([j, i , pivot_df.iloc[i, j]])
    if label == "max_drawdown":
        range_color = ["#00FF00", "#FFFF00", "#FF0000"][::-1]  # 绿-黄-红
    else:
        range_color = ["#00FF00", "#FFFF00", "#FF0000"]  # 绿-黄-红
    # 绘制热力图
    heatmap = (
        HeatMap()
        .add_xaxis(pivot_df.columns.tolist())
        .add_yaxis(
            f"{label}",
            pivot_df.index.tolist(),
            datas,
            label_opts=opts.LabelOpts(is_show=False),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title=f"{label}热力图"),
            visualmap_opts=opts.VisualMapOpts(
                min_=result_df[f"{label}"].min(),
                max_=result_df[f"{label}"].max(),
                is_calculable=True,
                range_color=range_color,
            ),
            tooltip_opts=opts.TooltipOpts(
                trigger="item",
                axis_pointer_type="cross"
            ),
        )
    )
    return heatmap


def create_data_table(df, title):
    """创建数据表格"""
    # 转换数据为表格格式
    headers = df.columns.tolist()
    rows = []
    for _, row in df.iterrows():
        rows.append([str(x) for x in row.tolist()])

    table = (
        Table()
        .add(headers, rows)
        .set_global_opts(
            title_opts=opts.ComponentTitleOpts(title=title, subtitle="")
        )
    )
    return table


def create_line_chart(df, title, x_label, y_label1, y_label2, y_label3, oi_l, oi_s):
    """创建折线图"""
    line = (
        Line()
        .add_xaxis(df[x_label].tolist())
        .add_yaxis(f"oi_l={oi_l},oi_s={oi_s}", df[y_label1].tolist(),
                  is_smooth=True,
                  label_opts=opts.LabelOpts(is_show=False))
        .add_yaxis("hold_btc", df[y_label2].tolist(),
                  is_smooth=True,
                  label_opts=opts.LabelOpts(is_show=False))
        .add_yaxis("oi_momentum",df[y_label3].tolist(),
                  is_smooth=True,
                  label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
            title_opts=opts.TitleOpts(title=title),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            yaxis_opts=opts.AxisOpts(
                axislabel_opts=opts.LabelOpts(formatter="{value}")
            )
        )
    )
    return line


# path = "D:/python_workspace/requirements/boll/part3/result/backtest_result_heatmap.csv"
path = "./result/backtest_result_heatmap.csv"
result_df = pd.read_csv(path)
page = Page(layout=Page.SimplePageLayout)

for label in ["annualized_shape", "annualized_return", "annualized_volatility", "turnover", "margin",
                     "fitness", "max_drawdown", "information_ratio", "calmar_ratio", "trading_counts",
                     "winning_rate", "profit_loss_ratio"]:
    heatmap = create_heatmap(result_df, label)
    page.add(heatmap)
for table_name in ["annualized_shape", "margin", "calmar_ratio", "information_ratio"]:
    path = f"./top3/{table_name}.csv"
    table_df = pd.read_csv(path)
    table = create_data_table(table_df, "annualized_shape_top3")
    page.add(table)

    for _, row in table_df.iterrows():
        oi_l = int(row['oi_l'])
        oi_s = int(row['oi_s'])

        path = f"./nav/nav_{oi_l}_{oi_s}.csv"
        nav_df = pd.read_csv(path)
        line = create_line_chart(nav_df, "净值曲线", "date", f"nav", "nav(hold_btc)","oi_momentum", oi_l, oi_s)
        page.add(line)

# 生成并保存
page.render("2025-09-12-1回测结果2.html")